package com.jch.ts;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.traffic.GlobalTrafficShapingHandler;

import java.nio.charset.StandardCharsets;

public class TsServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        p.addLast(new LineBasedFrameDecoder(1024)); // 基于换行符 \r\n
        // (作用于全局所有channel| 该Handler使用@Sharable注解标记 为全局所有通道共享)流量整形： 设置 写上限 | 读上限 单位bytes/s |
        p.addLast(new GlobalTrafficShapingHandler(ch.eventLoop().parent(),10,10));
        // 字符串编解码
        p.addLast(new StringDecoder(StandardCharsets.UTF_8));
        p.addLast(new StringEncoder(StandardCharsets.UTF_8));

        // 自定义数据处理器
        p.addLast(new TsServerHandler());
    }
}
